/**
 * Copyright (c) 2014-2015, Suredy technology Co., Ltd. All rights reserved.
 * @author ZhangMaoren 
 * @since 2015年4月15日
 * @version 0.1
 */
package com.suredy.config.ctrl;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.suredy.config.model.DocTemplate;
import com.suredy.config.model.ESeal;
import com.suredy.config.model.FileTypeConfig;
import com.suredy.config.model.SN;
import com.suredy.config.service.DocTemplateSrv;
import com.suredy.config.service.ESealSrv;
import com.suredy.config.service.FileTypeConfigSrv;
import com.suredy.config.service.SNSrv;
import com.suredy.core.ctrl.BaseCtrl;


/**
 * @author ZhangMaoren
 *
 */
@Controller
@RequestMapping(value="config")
public class FileTypeConfigCtrl extends BaseCtrl {
	private final static Logger log = LoggerFactory.getLogger(FileTypeConfigCtrl.class);
	private final String MSG_FILETYPECONFIG_NOT_FOUND = "未能获取文件类型配置数据，请查看log获取更多错误信息！";
	
	@Autowired
	private FileTypeConfigSrv ftconfig;
	
	@Autowired 
	private SNSrv snSrv;
	
	@Autowired
	private ESealSrv esSrv;
	
	@Autowired
	private DocTemplateSrv docSrc;

	/**
	 * get notice list
	 * 
	 * @param type
	 * @param page
	 * @return
	 */
	@RequestMapping(value = "filetypeconfig-list")
	public ModelAndView getAll() {
		
		ModelAndView view = new ModelAndView("config/filetypeconfig-list");
		List<FileTypeConfig> data = this.ftconfig.getAll();
		view.addObject("data", data);

		return view;
	}
	
	

	/**
	 * get filetypeconfig form
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping("filetypeconfig-form")
	public ModelAndView get(String id) {	
		ModelAndView view = new ModelAndView("config/filetypeconfig-form");
		List<SN> sNs = this.snSrv.getAll();
		view.addObject("sNs", sNs);
		
		List<ESeal> eSeals = esSrv.getAll();
		view.addObject("eSeals", eSeals);
		
		List<DocTemplate> docTemplates = docSrc.getAll();
		view.addObject("docTemplates", docTemplates);
		
		//TODO 表单ID，流程ID
		
		if (StringUtils.isBlank(id)) {
			log.info("Parameter String[id] is blank, try to create new file type configuration.");
			return view;
		} 
		

		FileTypeConfig data = this.ftconfig.get(id);
		if (data == null) {
			log.error("Can not get file type configuration data by id[" + id + "].");
			return this.gotoErrorPage(this.MSG_FILETYPECONFIG_NOT_FOUND);
		}
		view.addObject("data", data);
		
		return view;
	}
	
	@RequestMapping("filetypeconfig-save")
	@ResponseBody
	public String save(HttpServletRequest request) {
		String id = request.getParameter("id");
		String name = request.getParameter("name");
		String appId = request.getParameter("appId");
		String newURI = request.getParameter("newURI");
		String openURI = request.getParameter("openURI");
		String flowUN = request.getParameter("openURI");
		String templateId = request.getParameter("templateId");
		String snmodeId = request.getParameter("snmodeId");
		String esealId = request.getParameter("esealId");
		String description = request.getParameter("description");
		
		if (StringUtils.isBlank(name)) {
			log.error("Parameter String[name] is blank.");
			return null;
		}
		FileTypeConfig n = new FileTypeConfig();
		if (id != null){
			n.setId(id);
		}
		n.setAppId(appId);
		n.setEsealId(esealId);
		n.setNewURI(newURI);
		n.setOpenURI(openURI);
		n.setFlowUN(flowUN);
		n.setSnmodeId(snmodeId);
		n.setTemplateId(templateId);
		n.setDescription(description);
		n.setName(name);
		
		n = this.ftconfig.saveOrUpdate(n);

		return n == null ? null : n.getId();
	}


}